package cn.gcBlock.rest;

import cn.gcBlock.service.BlockNodeService;
import cn.gcBlock.service.BlockNodeTransService;
import cn.gcBlock.utils.MathUtils;
import cn.gcBlock.po.Node;
import cn.vo.BlockVo;
import cn.vo.ServerVo;
import cn.vo.ServersVo;
import cn.gcBlock.dto.NodeDTO;
import cn.gcBlock.po.constant.NodeType;
import cn.gcBlock.dto.NodeQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author：dapan
 * @Description:cadota
 * @Date:Create in 17:39 2018/5/5
 * @Modifird By:
 */
@Api(tags = "区块签名转发模块（共识维持机制）")
@RestController
@RequestMapping
public class RestBlockImpl implements RestBlockService {
    @Resource
    BlockNodeService blockNodeService;
    @Resource
    BlockNodeTransService blockNodeTransService;

    @ApiOperation(value = "发送签名账单")
    public ResponseEntity transfer(@RequestBody NodeDTO resource) {
        blockNodeTransService.transBlock(resource);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 获取余额，及钱包状态
     */
    @ApiOperation(value = "获取区块余额")
        public ResponseEntity<BlockVo> getBlockAddressInfo(@RequestParam(value = "publicKey") String publicKey) {
        double remain = blockNodeService.getRemainingSum(publicKey);
        BlockVo blockVo = new BlockVo();
        blockVo.setRemain(remain);
        blockVo.setPublicKey(publicKey);
        boolean isPublicServer = blockNodeService.isPublicServer(publicKey);
        blockVo.setTps(MathUtils.getRandom(1000,2000));
        blockVo.setPublicServer(isPublicServer);
        return new ResponseEntity(blockVo, HttpStatus.OK);
    }

    /**
     * 获取余额，及钱包状态
     */
    @ApiOperation(value = "获取服务区块")
    public ResponseEntity servers(NodeQueryCriteria criteria, Pageable pageable) {
        ServersVo serversVo = new ServersVo();
        List<ServerVo> list = new ArrayList<>();
        serversVo.setServers(list);
        Page<Node> page = blockNodeService.queryNodes(criteria,pageable);
         for(Node item:page.toList()){
             ServerVo serverVo = new ServerVo();
             BeanUtils.copyProperties(item,serverVo);
             serverVo.setRemain(blockNodeService.getRemainingSum(serverVo.getPublicKey()));
             serverVo.setTps(MathUtils.getRandom(1000,5000));
             NodeQueryCriteria nodeQueryCriteria = new NodeQueryCriteria();
             nodeQueryCriteria.setPublicKey(item.getPublicKey());
             nodeQueryCriteria.setType(NodeType.NEWS.getCode());
             Pageable pages = PageRequest.of(0,2);
             Page<Node> newsPage = blockNodeService.queryNodes(nodeQueryCriteria,pages);
             serverVo.setNodes(newsPage.toList());
             list.add(serverVo);
         }
        serversVo.setTotal(page.getTotalPages());
        return new ResponseEntity(serversVo, HttpStatus.OK);
    }

    /**
     * 获取区块详情
     */
    @ApiOperation(value = "获取区块信息")
    public ResponseEntity getBlockAddressInfo(NodeQueryCriteria criteria) {
        NodeDTO nodeDTO = blockNodeService.findById(criteria.getSignature());
        return new ResponseEntity(nodeDTO, HttpStatus.OK);
    }

    @ApiOperation(value = "查询区块列表")
    public ResponseEntity getBlockNodes(NodeQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(blockNodeService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation(value = "查询币种列表")
    public ResponseEntity getSymbols() {
        return new ResponseEntity(blockNodeService.findSymbols(), HttpStatus.OK);
    }
}
